#include "Tokenizer.h"
using namespace util;

string Tokenizer::getNextWord()
{
	char word[256];
	memset(word,0,sizeof(char)*256);
	fscanf(m_file,"%255s",word);
	return string(word);
}

void Tokenizer::rewind()
{
	fseek(m_file,0,SEEK_SET);
}

float Tokenizer::getNextFloat()
{
	float result;
	fscanf(m_file,"%f",&result);
	return result;
}

void Tokenizer::readNextFloatTriplet(util::Triplet<float> & result)
{
	fscanf(m_file,"%f %f %f",&(result[0]), &(result[1]), &(result[2]) );
}

Tokenizer::Tokenizer(string filename)
{
	m_file = fopen(filename.c_str(),"r");
	if(m_file == NULL)
		throw TokenizerException("File could not be opened");
}

Tokenizer::~Tokenizer()
{
	fclose(m_file);
}

void Tokenizer::checkToken(string expected, string received)
{
	if(expected != received)
	{
		string text = "Expected token \""+expected+"\", but received token \""+received+"\"";
		throw TokenizerException(text);
	}
}

